﻿using CJAtech.BBS.Process;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bigger.Framework;
using Bigger.Web.Helper;

namespace CJAtech.BBS.Web.Controllers
{
    [BBSCorsFilter]
    [BBSConfigFilter("bm")]
    //[SSOAuthFilter(BBSPowerType.CheckLogin)]
    public partial class BMController : BBSBaseController
    {
        public override BBSBaseProcess CreateBBSProcess()
        {
            return new BBSBaseProcess("bm");
        }

        [SSOAuthFilter(BBSPowerType.None)]
        public ActionResult LoginOut()
        {
            SSOProcess objSSOProcess = new SSOProcess(CurrentBBSProcess.CurrentCommunityConfig);
            string token = CookieHelper.GetCookieValue(CurrentBBSProcess.CurrentCommunityConfig.ConfigCode + "SSOToken");
            InvokeResult objInvokeResult = new InvokeResult();
            if (token.IsNull())
            {
                objInvokeResult.ResultCode = "TokenNull";
                objInvokeResult.ResultMessage = "注销失败，Token为空";
                return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
            }
            objSSOProcess.LoginOut(token);
            objInvokeResult.ResultMessage = "注销成功";
            return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
        }

        [SSOAuthFilter(BBSPowerType.None)]
        public ActionResult Target(string token, string sign, string targetUrl)
        {
            if (targetUrl.IsNull())
            {
                return Http404();
            }
            targetUrl = targetUrl.DecodeUrl();

            SSOProcess objSSOProcess = new SSOProcess(CurrentBBSProcess.CurrentCommunityConfig);

            if (!objSSOProcess.ValidTicket())
            {
                return Content("请求不合法");
            }

            UserInfo objUserInfo = objSSOProcess.GetUserInfoConvert(token);
            if (objUserInfo == null)
            {
                return Redirect(objSSOProcess.CreateRedirectConvertUrl(targetUrl));
            }

            CookieHelper.SetCookieValue(token, CurrentBBSProcess.CurrentCommunityConfig.ConfigCode + "SSOToken");

            return Redirect(targetUrl);
        }
    }
}
